/*
 * 文件名： MerchantSettledPabManager.java
 * 
 * 工程名称: bis-manager
 *
 * Gopay
 *
 * 创建日期： 2018年1月3日
 *
 * Copyright(C) 2018, by www.gopay.com.cn Inc.All rights reserved.
 *
 * 原始作者: 宣广海
 *
 */
package com.gopay.bis.manager.bank.merchant;

import javax.annotation.Resource;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.bank.PabConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.MerchantSettledRequest;
import com.gopay.common.domain.bank.merchant.MerchantSettledMessage;
import com.gopay.common.domain.bank.merchant.MerchantSettledOrgReqMsg;
import com.gopay.common.domain.bank.merchant.contract.req.ContractAddReqMsg;
import com.gopay.common.domain.bank.merchant.contract.req.ContractDeleteReqMsg;
import com.gopay.common.domain.bank.merchant.contract.req.ContractEditReqMsg;
import com.gopay.common.domain.bank.merchant.contract.req.ContractPayFeeReqMsg;
import com.gopay.common.domain.bank.merchant.contract.req.ContractRelateReqMsg;
import com.gopay.common.domain.bank.merchant.mer.req.MechantAddReqMsg;
import com.gopay.common.domain.bank.merchant.mer.req.MerchantDeleteReqMsg;
import com.gopay.common.domain.bank.merchant.mer.req.MerchantEditReqMsg;
import com.gopay.common.domain.bank.merchant.shop.req.ShopAddReqMsg;
import com.gopay.common.domain.bank.merchant.shop.req.ShopDeleteReqMsg;
import com.gopay.common.domain.bank.merchant.shop.req.ShopEditReqMsg;
import com.gopay.common.domain.bank.merchant.shop.req.ShopOpenIdReqMsg;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.FastJsonUtil;
import com.gopay.remote.bank.businesses.MerchantSettledService;

/**
 * 商户入驻-平安银行渠道报文拼装处理
 *
 * @author 宣广海
 *
 * @version
 *
 * @since 2018年1月3日
 */
public class MerchantSettledPabManager {
	/**
	 * 日志对象
	 */
	private static Logger logger = LoggerFactory.getLogger(MerchantSettledPabManager.class);
	/**
	 * 注入对象
	 */
	@Resource(name = "merSettledPabServiceClient")
	private MerchantSettledService merSettledPabServiceClient;

	/**
	 * 
	 * 功能描述：添加商户
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月5日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object merchantAdd(MerchantSettledRequest<MechantAddReqMsg> request) throws GopayException {

		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			MechantAddReqMsg mechantAddMsg = request.getBody();
			// 是否自动提审1为自动提审，其他或者空为需提审
			mechantAddMsg.setMts(PabConstants.MTS);
			// 后续需要补存缺失参数
			mechantAddMsg.setCheck_card("2");
			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(mechantAddMsg));
			message.setInterfaceName(PabConstants.MERCHANT_ADD);
			logger.info("pab merchantAdd build datajson= :" + buildSignPlain(mechantAddMsg));
		} catch (Exception e) {
			logger.error("pab merchantAdd build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2200);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：编辑商户
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月6日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object merchantEdit(MerchantSettledRequest<MerchantEditReqMsg> request) throws GopayException {

		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			MerchantEditReqMsg mechantEditMsg = request.getBody();
			// 后续需要补存缺失参数
			mechantEditMsg.setCheck_card("2");
			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(mechantEditMsg));
			message.setInterfaceName(PabConstants.MERCHANT_EDIT);
			logger.info("pab merchantEdit build datajson= :" + buildSignPlain(mechantEditMsg));
		} catch (Exception e) {
			logger.error("pab merchantEdit build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2201);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：删除商户
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月6日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object merchantDelete(MerchantSettledRequest<MerchantDeleteReqMsg> request) throws GopayException {

		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			MerchantDeleteReqMsg mechantDeleteMsg = request.getBody();
			// 后续需要补存缺失参数

			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(mechantDeleteMsg));
			message.setInterfaceName(PabConstants.MERCHANT_DELETE);
			logger.info("pab merchantDelete build datajson= :" + buildSignPlain(mechantDeleteMsg));
		} catch (Exception e) {
			logger.error("pab merchantDelete build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2202);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：获取商户分类
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月6日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object merchantClass(MerchantSettledRequest request) throws GopayException {
		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			message = buildParam(message, request);
			message.setInterfaceName(PabConstants.MERCHANT_CLASS);
			message.setPlainMessage(null);
		} catch (Exception e) {
			logger.error("pab merchantClass build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2203);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：获取商户城市编码
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月6日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object merchantCity(MerchantSettledRequest request) throws GopayException {
		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			message = buildParam(message, request);
			message.setInterfaceName(PabConstants.MERCHANT_CITY);
			message.setPlainMessage(null);
		} catch (Exception e) {
			logger.error("pab merchantCity build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2204);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：添加门店
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月8日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object shopAdd(MerchantSettledRequest<ShopAddReqMsg> request) throws GopayException {

		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			ShopAddReqMsg shopAddMsg = request.getBody();
			// 是否自动提审1为自动提审，其他或者空为需提审
			shopAddMsg.setMts(PabConstants.MTS);
			// 后续需要补存缺失参数

			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(shopAddMsg));
			message.setInterfaceName(PabConstants.SHOP_ADD);
			logger.info("pab shopAdd build datajson= :" + buildSignPlain(shopAddMsg));
		} catch (Exception e) {
			logger.error("pab shopAdd build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2205);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：编辑门店
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月8日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object shopEdit(MerchantSettledRequest<ShopEditReqMsg> request) throws GopayException {

		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			ShopEditReqMsg shopEditMsg = request.getBody();
			// 后续需要补存缺失参数

			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(shopEditMsg));
			message.setInterfaceName(PabConstants.SHOP_EDIT);
			logger.info("pab shopEdit build datajson= :" + buildSignPlain(shopEditMsg));
		} catch (Exception e) {
			logger.error("pab shopEdit build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2206);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：删除门店
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月8日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object shopDelete(MerchantSettledRequest<ShopDeleteReqMsg> request) throws GopayException {

		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			ShopDeleteReqMsg shopDeleteMsg = request.getBody();
			// 后续需要补存缺失参数

			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(shopDeleteMsg));
			message.setInterfaceName(PabConstants.SHOP_DELETE);
			logger.info("pab shopDelete build datajson= :" + buildSignPlain(shopDeleteMsg));
		} catch (Exception e) {
			logger.error("pab shopDelete build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2207);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：获取门店openId及密钥
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月8日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object shopOpenId(MerchantSettledRequest<ShopOpenIdReqMsg> request) throws GopayException {

		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			ShopOpenIdReqMsg shopOpenIdMsg = request.getBody();
			// 后续需要补存缺失参数

			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(shopOpenIdMsg));
			message.setInterfaceName(PabConstants.SHOP_OPENID);
			logger.info("pab shopOpenId build datajson= :" + buildSignPlain(shopOpenIdMsg));
		} catch (Exception e) {
			logger.error("pab shopOpenId build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2208);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：获取合同支付方式列表
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月9日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object contractPayList(MerchantSettledRequest request) throws GopayException {
		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			message = buildParam(message, request);
			message.setInterfaceName(PabConstants.CONTRACT_PAYLIST);
			message.setPlainMessage(null);
		} catch (Exception e) {
			logger.error("pab contractPayList build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2209);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：获取合同支付费率
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月9日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object contractPayFee(MerchantSettledRequest<ContractPayFeeReqMsg> request) throws GopayException {
		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			ContractPayFeeReqMsg contractPayFeeReqMsg = request.getBody();
			// 后续需要补存缺失参数

			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(contractPayFeeReqMsg));
			message.setInterfaceName(PabConstants.CONTRACT_PAYFEE);
			logger.info("pab contractPayFee build datajson= :" + buildSignPlain(contractPayFeeReqMsg));
		} catch (Exception e) {
			logger.error("pab contractPayFee build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2210);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：添加合同
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月9日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object contractAdd(MerchantSettledRequest<ContractAddReqMsg> request) throws GopayException {
		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			ContractAddReqMsg contractAddMsg = request.getBody();
			// 是否自动提审1为自动提审，其他或者空为需提审
			contractAddMsg.setMts(PabConstants.MTS);
			// 后续需要补存缺失参数
			logger.info("参数：" + request.getBody().getService_phone());
			logger.info("参数2：" + ToStringBuilder.reflectionToString(request.getBody()));
			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(contractAddMsg));
			message.setInterfaceName(PabConstants.CONTRACT_ADD);
			logger.info("pab contractAdd build datajson= :" + buildSignPlain(contractAddMsg));
		} catch (Exception e) {
			logger.error("pab contractAdd build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2211);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：编辑合同
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月9日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object contractEdit(MerchantSettledRequest<ContractEditReqMsg> request) throws GopayException {
		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			ContractEditReqMsg contractEditMsg = request.getBody();
			// 后续需要补存缺失参数

			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(contractEditMsg));
			message.setInterfaceName(PabConstants.CONTRACT_EDIT);
			logger.info("pab contractEdit build datajson= :" + buildSignPlain(contractEditMsg));
		} catch (Exception e) {
			logger.error("pab contractEdit build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2212);
		}
		return merSettledPabServiceClient.handle(message);
	}

	/**
	 * 
	 * 功能描述：删除合同
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月9日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object contractDelete(MerchantSettledRequest<ContractDeleteReqMsg> request) throws GopayException {
		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			ContractDeleteReqMsg contractDeleteMsg = request.getBody();
			// 后续需要补存缺失参数

			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(contractDeleteMsg));
			message.setInterfaceName(PabConstants.CONTRACT_DELETE);
			logger.info("pab contractDelete build datajson= :" + buildSignPlain(contractDeleteMsg));
		} catch (Exception e) {
			logger.error("pab contractDelete build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2213);
		}
		return merSettledPabServiceClient.handle(message);
	}
	/**
	 * 
	 * 功能描述：合同关联门店
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月9日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object contractRelateShop(MerchantSettledRequest<ContractRelateReqMsg> request) throws GopayException {
		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			ContractRelateReqMsg contractRelateMsg = request.getBody();
			// 后续需要补存缺失参数

			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(contractRelateMsg));
			message.setInterfaceName(PabConstants.CONTRACT_RELATE);
			logger.info("pab contractRelateShop build datajson= :" + buildSignPlain(contractRelateMsg));
		} catch (Exception e) {
			logger.error("pab contractRelateShop build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2214);
		}
		return merSettledPabServiceClient.handle(message);
	}
	/**
	 * 
	 * 功能描述：上传文件
	 *
	 * @param request
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2018年1月9日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object uplodeFile(MerchantSettledRequest<MerchantSettledOrgReqMsg> request) throws GopayException {
		MerchantSettledMessage message = new MerchantSettledMessage();
		try {
			MerchantSettledOrgReqMsg orgMsg = request.getBody();
			// 后续需要补存缺失参数
			message = buildParam(message, request);
			message.setPlainMessage(buildSignPlain(orgMsg));
			message.setInterfaceName(PabConstants.FILE_UPLODE);
			//logger.info("pab uplodeFile build datajson= :" + buildSignPlain(orgMsg));
			return merSettledPabServiceClient.handle(message);
		} catch (Exception e) {
			logger.error("pab uplodeFile build datajson error" + e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E2215);
		}
		
	}
	public String buildSignPlain(Object obj) {
		return FastJsonUtil.toJSONString(obj);
	}

	public MerchantSettledMessage buildParam(MerchantSettledMessage message, MerchantSettledRequest<?> request) {
		message.setLang(PabConstants.LANG);
		message.setMerId(request.getMerId());
		message.setMerKey(request.getMerKey());
		message.setUrl(request.getUrl());
		// 随机字符串，不能少于32位（字母、数字）
		message.setRandStr(RandomStringUtils.randomAlphanumeric(32));
		// Linx时间戳，1970-01-01至现在的秒数
		message.setTimestamp(String.valueOf(System.currentTimeMillis() / 1000));
		return message;
	}
}
